/* Copyright 2012 Alejandro Alonso Mayo
This file is part of jmtools.

jmtools is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

jmtools is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with jmtools.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <jmtools/Log.h>

#include <jmtools/DateTime.h>

#include <iostream>

using namespace std;
using namespace JMTools::Time;

namespace JMTools
{
	namespace Logs
	{
		Log::Log(std::string name,LogType type,bool cmd,bool restricted)
		{
			this->name = name;
			this->file.open(name.data());
			this->type = type;
		        this->cmd = cmd;
			this->restricted = restricted;
	        this->logMessage("Created log file: "+name+"");
		}
	
		Log::~Log()
		{
			this->logMessage("Log file "+this->name+" closed.");
			this->file.close();
		}

		std::string Log::getName()
		{
			return this->name;
		}

		void Log::logMessage(std::string message)
		{
			this->file<<DateTime::now()<<": "<<message<<endl;
		}

		void Log::logMessage(std::string message,LogType type)
		{
			if ((!this->restricted && type>=this->type) || type==this->type)
			{
				string mtype;
				switch(type)
				{
				case LT_INFORMATION:
					mtype = "Information";
					break;
				case LT_WARNING:
					mtype = "Warning";
					break;
				default:
					mtype = "Error";
				}
				
				this->file<<DateTime::now()<<": "<<mtype<<": "<<message<<endl;
				if (this->cmd)
				        cout<<DateTime::now()<<": "<<mtype<<": "<<message<<endl;
			}
		}
	}
}
